package com.taotao.manage.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taotao.common.pojo.EasyUIDataGridResult;
import com.taotao.common.service.RedisService;
import com.taotao.manage.mapper.ContentMapper;
import com.taotao.manage.pojo.Content;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ContentServiceImpl extends BaseServiceImpl<Content> implements ContentService {
    @Autowired
    private ContentMapper contentMapper;
    @Autowired
    private RedisService redisService;
    //处理json字符串的工具类
    private static final ObjectMapper MAPPER = new ObjectMapper();

    @Value("${CONTENT_CATEGORY_BIG_AD_ID}")
    private Long CONTENT_CATEGORY_BIG_AD_ID;

    @Value("${TAOTAO_PORTAL_INDEX_BIG_AD_NUMBER}")
    private Integer TAOTAO_PORTAL_INDEX_BIG_AD_NUMBER;

    private static String REDIS_BIG_AD_KEY="bigAdData";
    private static Integer REDIS_BIG_AD_EXPIRE_TIME=86400;
    /**
     * 根据内容Id分页查询内容列表
     */
    @Override
    public EasyUIDataGridResult queryContentListByCategoryId(Long categoryId, Integer page, Integer rows) {
        Example example = new Example(Content.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("categoryId", categoryId);
        example.orderBy("updated").desc();
        PageHelper.startPage(page, rows);
        List<Content> list = contentMapper.selectByExample(example);
        PageInfo<Content> pageInfo = new PageInfo<>(list);
        return new EasyUIDataGridResult(pageInfo.getTotal(), pageInfo.getList());

    }

    /**
     * 获取门户系统首页大广告数据
     */
    @Override
    public String queryPortalBigAdData() throws Exception {
        try {
            String jsonStr = redisService.get(REDIS_BIG_AD_KEY);
            if(StringUtils.isNotBlank(jsonStr)){
                return jsonStr;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        EasyUIDataGridResult dataGridResult = queryContentListByCategoryId(CONTENT_CATEGORY_BIG_AD_ID, 1, TAOTAO_PORTAL_INDEX_BIG_AD_NUMBER);
        List<Content> contentList = dataGridResult.getRows();
        List<Map<String, Object>> resultList = new ArrayList<>();
        Map<String, Object> map = null;
        if (contentList != null) {
            for (Content content : contentList) {
                map = new HashMap<>();
                map.put("alt", content.getTitle());
                map.put("height", 240);
                map.put("heightB", 240);
                map.put("href", content.getUrl());
                map.put("src", content.getPic());
                map.put("srcB", content.getPic());
                map.put("width", 670);
                map.put("widthB", 550);
                resultList.add(map);
            }
        }
        String str = MAPPER.writeValueAsString(resultList);

        try {
            redisService.setex(REDIS_BIG_AD_KEY,REDIS_BIG_AD_EXPIRE_TIME,str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return str;
    }
}
